package controller

import (
	"data-system/common"
	"data-system/dao"
	"data-system/excel"
	"data-system/models"
	"log"
	"strconv"

	"github.com/gin-gonic/gin"
)

func QueryNodeTypeDataList(c *gin.Context) {
	nodeType := c.Query("nodeType")
	conflict := excel.IsConflict(nodeType)
	if conflict {
		c.JSON(models.Fail(common.ErrorConflict, nodeType+"存在冲突问题，请先解决"))
		return
	}
	nodes, err := dao.QueryAll(nodeType)
	if err != nil {
		log.Println(err.Error())
		c.JSON(models.FailData(err.Error()))
		return
	}
	c.JSON(models.OkData(nodes))
}

func QueryNodeTypeDataDetail(c *gin.Context) {
	nodeType := c.Query("nodeType")
	id, _ := strconv.Atoi(c.Query("id"))
	conflict := excel.IsConflict(nodeType)
	if conflict {
		c.JSON(models.Fail(common.ErrorConflict, nodeType+"存在冲突问题，请先解决"))
		return
	}
	nodes, err := dao.QueryById(nodeType, int64(id))
	if err != nil {
		log.Println(err.Error())
		c.JSON(models.FailData(err.Error()))
		return
	}
	c.JSON(models.OkData(nodes))
}
